import pgzrun
import random

WIDTH = 500
HEIGHT = 700
numbers = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  # 数字
# 让随机一个格子变为2
a = random.randint(0, 3)
b = random.randint(0, 3)
c = random.randint(1, 2)
numbers[a][b] = 2 * c
fl = 0


def f1():  # 向上、向下
    for j in range(4):
        for i in range(3):
            if numbers[i][j] != 0:
                t = 1
                while i + t <= 3:
                    if numbers[i + t][j] == 0:
                        t += 1
                        continue
                    elif numbers[i + t][j] == numbers[i][j]:
                        numbers[i][j] *= 2
                        numbers[i + t][j] = 0
                        break
                    else:
                        break


def f2():  # 向左、向右
    for i in range(4):
        for j in range(3):
            if numbers[i][j] != 0:
                t = 1
                while j + t <= 3:
                    if numbers[i][j + t] == 0:
                        t += 1
                        continue
                    elif numbers[i][j + t] == numbers[i][j]:
                        numbers[i][j] *= 2
                        numbers[i][j + t] = 0
                        break
                    else:
                        break


def draw():
    global fl
    screen.clear()
    screen.blit('bg', [0, 100])
    # 绘制数字
    for i in range(4):
        for j in range(4):
            if numbers[i][j] != 0:
                x = 125 * j + 62.5
                y = 125 * i + 187.5
                screen.draw.text(str(numbers[i][j]), (x, y), fontsize=50)
    if fl == 1:
        screen.draw.text('game over', (0, 0), fontsize=50)


def on_key_down(key):
    global fl
    if key == keys.RIGHT:  # 向右
        f2()
        for i in range(4):
            n = 3
            for j in range(3, -1, -1):
                if numbers[i][j] != 0:
                    if j != 3 and n != j:
                        numbers[i][n] = numbers[i][j]
                        numbers[i][j] = 0
                    n -= 1
    elif key == keys.LEFT:  # 向左
        f2()
        for i in range(4):
            n = 0
            for j in range(4):
                if numbers[i][j] != 0:
                    if j != 0 and n != j:
                        numbers[i][n] = numbers[i][j]
                        numbers[i][j] = 0
                    n += 1
    elif key == keys.DOWN:  # 向下
        f1()
        for j in range(4):
            n = 3
            for i in range(3, -1, -1):
                if numbers[i][j] != 0:
                    if i != 3 and n != i:
                        numbers[n][j] = numbers[i][j]
                        numbers[i][j] = 0
                    n -= 1
    elif key == keys.UP:  # 向上
        f1()
        for j in range(4):
            n = 0
            for i in range(4):
                if numbers[i][j] != 0:
                    if i != 0 and n != i:
                        numbers[n][j] = numbers[i][j]
                        numbers[i][j] = 0
                    n += 1
    f = 0
    for i in numbers:
        for j in i:
            if j == 0:
                f = 1
                break
    # 让随机一个格子变为2
    if f == 1:
        a1 = random.randint(0, 3)
        b1 = random.randint(0, 3)
        while numbers[a1][b1] != 0:
            a1 = random.randint(0, 3)
            b1 = random.randint(0, 3)
        c1 = random.randint(1, 2)
        numbers[a1][b1] = 2 * c1
    else:
        for i in range(4):
            for j in range(4):
                if j != 3:
                    if numbers[i][j] == numbers[i][j + 1]:
                        f = 1
                if i != 3:
                    if numbers[i][j] == numbers[i + 1][j]:
                        f = 1
        if f == 0:
            fl = 1


pgzrun.go()
